<template>
	<view class="time-order-item">
		<view class="top">
			<view class="top-header">
				<uni-icons type="calendar" size="20"></uni-icons>
				<!-- <text v-if="item.is_recommend==1">[首页推荐]</text> -->
				<text class="name">{{ item.date }} ({{ item.time }})</text>
			</view>
			<view class="">
				<uni-icons type="email" size="20"></uni-icons>
				<text class="name">复制资料</text>
			</view>
		</view>
		<view class="container">
			<view class="row">
				<!-- 状态0=使用中1=已停止2=已关闭3=首页推荐4=置顶 -->
				<text class="label">状态：</text>
				<text class="green" v-if="item.status==0">预约成功， <text class="tips">未核销</text> </text>
				<text class="green" v-if="item.status==1">已取消</text>
				<text class="green" v-if="item.status==2">已核销</text>
				<text class="green" v-if="item.status==3">未核销</text>
			</view>
			<view class="row">
				<text class="label">姓名：</text><text>{{item.name}}</text>
			</view>
			<view class="row">
				<text class="label">电话：</text><text>{{ item.phone }}</text>
			</view>
			<view class="row">
				<text class="label">提交：</text><text>{{ item.create_time}}</text>
			</view>
			<view class="btns">
				<view class="btn" @click="goCheck">
					<text class="iconfont icon-shezhi"></text>
					<text>核销预约</text>
				</view>
				<view class="btn" @click="cancelCheck(item.id)">
					<text v-if="item.status==1">已取消</text>
					<text v-else>取消预约</text>
				</view>
				<view class="btn" @click="delItemById(item.id)">
					<text class="iconfont icon-zidonghexiao"></text>
					<text>删除</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as XLSX from '../../utils/excel.js'
	import { GetLi,FindByTelphone,CancelCheck,DelItemById } from '../../api/index.js'

	export default {
		name:"TimeOrderItem",
		props:{
			item:{
				type:Object,
				default:()=>{}
			},
			// index:{
			// 	type:Number,
			// 	default:0
			// }
		},
		data() {
			return {
				
			};
		},
		computed:{
			status(){
				let is =this.item.status
				// 状态0=使用中1=已停止2=已关闭3=首页推荐4=置顶
				// if(is==0){
				// 	return '使用中'
				// }
				// return this.item.status
				let res
				switch(is){
					case 0:
						res='使用中';
						break;
					case 1:
						res='已停止';
						break;
					case 2:
						res='已关闭';
						break;
				}
				console.log(res,'res')
				return res
			}
		},
		methods:{
			// 核销
			goCheck(){
				console.log('res,','ress')
				// uni.scanCode({
				// 	scanType:'qrCode',
				// 	success:function(res){
				// 		console.log(res,'ress')
				// 	}
				// })
				// 允许从相机和相册扫码
				uni.scanCode({
					// scanType:'qrCode',//无反应
					scanType:['qrCode'],
					success: async (res)=> {
						// this.qrcodeText = this.username+' '+this.type+' '+this.date+' '+this.myFormatData.username+' '+this.myFormatData.tel
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let results = res.result.split('_')
						console.log(results,'results')
						const { data:res1 } = await FindByTelphone(2)
						console.log(res1,'res1')
						if(results[0]==res1.data.username){
							
							if(res1.data.type==2){
								return uni.showToast({
									title:'已核销',
									icon:'error'
								})
							}
							if(res1.data.type==3){
								return uni.showToast({
									title:'已过期',
									icon:'error'
								})
							}
							if(res1.data.type==4){
								return uni.showToast({
									title:'系统取消',
									icon:'error'
								})
							}
							const { data:res } = await CheckoutCode({tel:2,type:2})
							console.log(res,'ccc')
							if(res.status==0){
								return uni.showToast({
									title:'核销成功',
									icon:'success'
								})
							}
						}else{
							return uni.showToast({
								title:'查询失败未找到',
								icon:'fail'
							})
						}
					}
				});
			},
			// checkTables(){
			// 	const fileName='打卡'
			// 	    const header = ['date', 'defaultType', 'device']
			// 	    const headerName = { date: '相识日期', defaultType: '类型', device: '姓名' }
			// 	    const ExcelData = [{ date: '2022-02-02', defaultType: '御姐', device: '学姐' }, { date: '2022-02-03', defaultType: '萝莉', device: '学妹' }]
			// 	    XLSX.excel_exprot(ExcelData, header, headerName,fileName)
			
			// },
			cancelCheck(id){
				console.log(id,'id')
				uni.showModal({
					title:'温馨提示',
					content:'确定要取消预约吗？',
					success:async(res)=> {
						if(res.confirm){
							const {data:res} = await CancelCheck(id,{status:1})
							console.log(res,'ree')
							if(res.status==0){
								this.$emit('updateCancel')
							}
						}
						if(res.cancel){
							
						}
					}
				})
			},
			// 删除
			async delItemById(id){
				console.log(this,'this')
				uni.showModal({
					title:'温馨提示',
					content:'确定要删除该条预约吗？',
					success:async(res)=> {
						if(res.confirm){
							console.log(this,'this1')
							const { data:res } =await DelItemById(id)
							console.log(res,'del')
							if(res.status==0){
								// let index = this.item.fin
								// this.item.splice(this.index,1)
								this.$emit('updateDel')
							
							}
						}
						if(res.cancel){
							
						}
					}
				})
				
			},
			checkTable(){
				uni.showActionSheet({
					itemList:['预约名单','导出名单Excel文件','管理员核销预约码','取消'],
					success: (res) => {
					          console.log('选择了第' + (res.tapIndex + 1) + '个选项');
							  if(res.tapIndex==0){
								  // GetLi().then(res=>{
									 //  console.log(res.data,'ff')
								  // })
								  // uni.$http({
									 //  url:'/api/helper/table',
									 //  method:'GET',
									 //  responseType: 'arraybuffer',
								  // }).then(res=>{
									 //  console.log(res,'g')
								  // })
								  uni.$http.get('/api/helper/table').then(res=>{
									  let resData = res.data
									  	console.log(res,'res',resData)
									  										
									  const fileManagerObj = uni.getFileSystemManager() // 获取全局的文件管理器
									             console.log(fileManagerObj,'o');
									             // 文件存储到本地的路径
									             const filePath = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.xlsx` 
									             fileManagerObj.writeFile({
									                 data: resData, // 拿到的arraybuffer数据
									                 filePath: filePath,
									                 encoding: 'binary',
									                 success: (res) => {
									                     console.log(res) // 成功了的话这里会打印 writeFile:ok
									                     this.viewDoc(filePath)
									                 }
									             })
									  // console.log(res,'hh')
								  })
								  uni.request({
								    url: 'http://192.168.120.43:3002/api/helper/table', // 替换为你的Node.js服务器地址
								    method: 'GET',
									sslVerify:false,
								    responseType: 'arraybuffer', // 设置响应类型为arraybuffer以接收二进制文件
								    success: (res) => {
										let resData = res.data
										console.log(res,'res',resData)
										
								      const fileManagerObj = uni.getFileSystemManager() // 获取全局的文件管理器
								                 console.log(fileManagerObj,'o');
								                 // 文件存储到本地的路径
								                 const filePath = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.xlsx` 
								                 fileManagerObj.writeFile({
								                     data: resData, // 拿到的arraybuffer数据
								                     filePath: filePath,
								                     encoding: 'binary',
								                     success: (res) => {
								                         console.log(res) // 成功了的话这里会打印 writeFile:ok
								                         this.viewDoc(filePath)
								                     }
								                 })
		
									  
									
								    },
								    fail: (error) => {
								      console.error('导出失败', error);
								    }
								  });
							  }
					        },
					        fail: (err) => {
					          console.log('弹窗取消');
					        }
					
				})
				
			},
			// 打开文件
			viewDoc(filePath) {
			    uni.openDocument({
			        // 直接打开
			        filePath: filePath, // 这里填上面写入本地的文件路径
			        fileType: 'xlsx',
			        showMenu: true, // 右上角是否有可以转发分享的功能，配不配随意
			        success: (res) => {
			            console.log('打开文档成功')
			        }
			    })
			},
		
		
		}
	}
</script>

<style lang="scss" scoped>
.time-order-item{
	width: 100%;
	margin-bottom: 30rpx;
	border: 1px solid #eee;
	border-radius: 10rpx;
	
	.top{
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background: #eee;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 10rpx;
		.top-header{
			.name{
				font-weight: bold;
				margin-left: 10rpx;
			}
		}
	}
	.container{
		background: #fff;
		padding-bottom: 30rpx;
		.row{
			margin-left: 50rpx;
			line-height: 60rpx;
			height: 60rpx;
			.label{
				font-weight: bold;
			}
		}
		.btns{
			display: flex;
			justify-content: space-around;
			margin-top: 20rpx;
			.btn{
				height: 70rpx;
				line-height: 70rpx;
				background: #e1e1e1;
				padding: 0rpx 20rpx;
				border-radius: 10rpx;
				.iconfont{
					margin-right: 6rpx;
				}
				.icon-zidonghexiao{
					color: orange!important;
				}
			}
		}
	}
}
</style>